﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using HCMServices.DTO;
using System.Collections.Generic;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;

namespace HCMServices.DAO
{
    /// <summary>
    /// Class to process for DULIEU Table 
    /// </summary>
    public class DataDAO
    {
        private MyDataContext db;

        /// <summary>
        /// Constructor of class
        /// </summary>
        /// <param name="connectString">String indentify how to connect database</param>
        public DataDAO(string connectString)
        {
            db = new MyDataContext(connectString); 
        }

        

        /// <summary>
        /// Function to search with Column MaDichVu
        /// </summary>
        /// <param name="strInput"></param>
        /// <returns></returns>
        public List<DataDTO> SearchWithSoNha(string strInput)
        {
            List<DataDTO> lstDuLieuDTO = new List<DataDTO>();

            IList<DULIEU> query = (from DULIEU Data in db.Data
                                   where (strInput.IndexOf(Data.SoNha) >= 0)
                                   select Data).ToList();

            ExportResult(lstDuLieuDTO, query);

            return lstDuLieuDTO;
        }


        /// <summary>
        /// Function to search with Column SoNha
        /// </summary>
        /// <param name="strInput">String contain SoNha which want to search</param>
        /// <returns>List of DULIEU row which has SoNha contain strInput</returns>
        public List<DataDTO> SearchWithMaDichVu(int id)
        {
            List<DataDTO> lstDuLieuDTO = new List<DataDTO>();

            IList<DULIEU> query = (from DULIEU Data in db.Data
                                   where Data.MaDichVu == id
                                   select Data).ToList();

            ExportResult(lstDuLieuDTO, query);

            return lstDuLieuDTO;
        }

        private static void ExportResult(List<DataDTO> lstDuLieuDTO, IList<DULIEU> query)
        {
            foreach (DULIEU d in query)
            {
                DataDTO temp = new DataDTO();
                temp.DataId = d.MaDuLieu;
                if (d.MaDichVu != null)
                    temp.ServiceId = (int)d.MaDichVu;
                temp.NumberOfHome = d.SoNha;
                if (d.MaTenDiaDiem != null)
                    temp.PlaceNameId = (int)d.MaTenDiaDiem;
                if (d.MaDuong != null)
                    temp.StreetId = (int)d.MaDuong;
                if (d.MaPhuong != null)
                    temp.WardId = (int)d.MaPhuong;
                if (d.MaQuanHuyen != null)
                    temp.DistrictId = (int)d.MaQuanHuyen;
                if (d.MaTinhThanh != null)
                    temp.CityId = (int)d.MaTinhThanh;
                if (d.KinhDo != null)
                    temp.Longitude = (double)d.KinhDo;
                if (d.ViDo != null)
                    temp.Latitude = (double)d.ViDo;
                
                temp.Comment = d.ChuThich;
                lstDuLieuDTO.Add(temp);
            }
        }

        /// <summary>
        /// Function to search in table DULIEU with more parameters, 
        /// this function will skip param if its value equal default number in myDataContext
        /// </summary>
        /// <param name="soNha">String contain value of SoNha which want to find</param>
        /// <param name="maDichVu">String contain value of MaDichVu which want to find</param>
        /// <param name="maTinhThanh">String contain value of MaTinhThanh which want to find</param>
        /// <param name="maQuanHuyen">String contain value of MaQuanHuyen which want to find</param>
        /// <param name="maPhuong">String contain value of MaPhuong which want to find</param>
        /// <param name="maDuong">String contain value of MaDuong which want to find</param>
        /// <param name="maTenDiaDiem">String contain value of MaTenDiaDiem which want to find</param>
        /// <returns>List of DULIEU row that have columns value match with params wasn't skip </returns>
        public List<DataDTO> Search(string soNha, int maDichVu, int maTinhThanh, int maQuanHuyen, int maPhuong, int maDuong, int maTenDiaDiem)
        {
            List<DataDTO> lstDuLieuDTO = new List<DataDTO>();
            int isCompare = MyDataContext.DefaultNumber;

            IList<DULIEU> query = db.Data
                .Where(c => ((null != soNha && soNha.Length > 0) ? soNha == c.SoNha : true) &&
                             ((isCompare == maDichVu) ? true : maDichVu == c.MaDichVu) &&
                             ((isCompare == maTinhThanh) ? true : maTinhThanh == c.MaTinhThanh) &&
                             ((isCompare == maQuanHuyen) ? true : maQuanHuyen == c.MaQuanHuyen) &&
                             ((isCompare == maPhuong) ? true : maPhuong == c.MaPhuong) &&
                             ((isCompare == maDuong) ? true : maDuong == c.MaDuong) &&
                             ((isCompare == maTenDiaDiem) ? true : maTenDiaDiem == c.MaTenDiaDiem))
                 .Select(c => c).ToList();

            ExportResult(lstDuLieuDTO, query);

            return lstDuLieuDTO;
        }
    }
}